<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('shop.advert.hotRecommend.title')"
                        type="string"
                        v-model="baTable.form.items!.title"
                        prop="title"
                        :placeholder="t('Please input field', { field: t('shop.advert.hotRecommend.title') })"
                    />
                    <FormItem
                        :label="t('shop.advert.hotRecommend.sub_title')"
                        type="string"
                        v-model="baTable.form.items!.sub_title"
                        prop="sub_title"
                        :placeholder="t('Please input field', { field: t('shop.advert.hotRecommend.sub_title') })"
                    />
                    <InputTag
                        :label="t('shop.advert.hotRecommend.tags')"
                        v-model="baTable.form.items!.tags"
                        tip="标签最大数量为3个,每个标签建议不超过6个文字"
                        limit="3"
                    />
                    <FormItem
                        :label="t('shop.advert.hotRecommend.image')"
                        type="image"
                        v-model="baTable.form.items!.image"
                        prop="image"
                        limit="1"
                        :input-attr="{
                            hideImagePlusOnOverLimit: true,
                        }"
                    />
                    <el-form-item :label="t('shop.advert.hotRecommend.classify_id')">
                        <el-select
                            v-model="baTable.form.items!.classify_id"
                            :placeholder="t('Please select field', { field: t('shop.advert.hotRecommend.classify_id') })"
                            style="width: 100%"
                            filterable
                            @visible-change="baTable.form.items!.goods_id = ''"
                            clearable
                        >
                            <el-option-group v-for="group in baTable.form.extend!.goodsClassifyData" :key="group.label" :label="group.label">
                                <el-option v-for="item in group.children" :key="item.value" :label="item.label" :value="item.value" />
                            </el-option-group>
                        </el-select>
                    </el-form-item>
                    <FormItem
                        :label="t('shop.advert.hotRecommend.goods_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.goods_id"
                        :key="baTable.form.items!.classify_id"
                        prop="goods_id"
                        :input-attr="{
                            pk: 'id',
                            field: 'name',
                            remoteUrl: '/admin/shop.goods.goods/index',
                            params: {
                                classify_id: baTable.form.items!.classify_id,
                            },
                        }"
                    />
                    <FormItem
                        :label="t('shop.advert.hotRecommend.status')"
                        type="switch"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :data="{ content: { '0': t('shop.advert.hotRecommend.status 0'), '1': t('shop.advert.hotRecommend.status 1') } }"
                        :input-attr="{
                            activeText: t('shop.advert.hotRecommend.status 1'),
                            inactiveText: t('shop.advert.hotRecommend.status 0'),
                            inlinePrompt: true,
                            style: {
                                '--el-switch-on-color': '#13ce66',
                                '--el-switch-off-color': '#ff4949',
                            },
                            width: 60,
                        }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import InputTag from '/@/components/shop/form/inputTag.vue'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    title: [buildValidatorData({ name: 'required', title: t('shop.advert.hotRecommend.title') })],
    image: [buildValidatorData({ name: 'required', title: t('shop.advert.hotRecommend.image') })],
    status: [buildValidatorData({ name: 'required', title: t('shop.advert.hotRecommend.status') })],
    create_time: [buildValidatorData({ name: 'date', title: t('shop.advert.hotRecommend.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('shop.advert.hotRecommend.update_time') })],
})

watch(
    () => baTable.form.items!.classify_id,
    (newVal) => {
        if (newVal == undefined) {
            baTable.form.items!.classify_id = ''
            baTable.form.items!.goods_id = ''
        }
    }
)
</script>

<style scoped lang="scss"></style>
